<?php


namespace App\Services;

use Illuminate\Support\Facades\DB;
use function EasyWeChat\Kernel\Support\get_client_ip;

/**
 * 支付
 * Class BasePayService
 * @package app\common\service
 */
class BasePayService
{
    // const IV_VERSION = '1.0';
    // const IUSERID = 13648;
    // const ITYPE = 'payment';
    // const APPKEY='6j5PACuknEPQgO30WEKGVZJ61fbhImhK';
    // const GAMEID = 5110785;
    // const GROUPID = 61932;
    const NOTIFY = '/api/pay/notify';
    const API_URL = 'http://api.64pay.com/api.html';
    const PAY_TYPE_MAP = [
        'WEIXIN',
        'ALIPAY',
    ];

    public $orderId; // 订单信息
    protected $ivVersion;
    protected $iuserId;
    protected $iType;
    protected $appKey;
    protected $gameId;
    protected $groupId;
    protected $payPlat;

    public function __construct($platName = '')
    {
        if ($platName) {
            $this->payPlat = DB::table('pay_platform')->where('plat_name', $platName)->first();
            if (!$this->payPlat) {
                throw new \Exception('支付平台错误');
            }
        } else {
            $platList = DB::table('pay_platform')->where('is_rand', 1)->get();
            if ($platList->count() == 0) {
                throw new \Exception('支付平台设置错误');
            }
            $rand = mt_rand(0, $platList->count()-1);
            $this->payPlat = $platList[$rand];
        }
        $this->ivVersion = $this->payPlat->pay_version;
        $this->iuserId = (int)$this->payPlat->user_id;
        $this->iType = $this->payPlat->type_id;
        $this->appKey = $this->payPlat->app_key;
        $this->gameId = (int)$this->payPlat->game_id;
        $this->groupId = (int)$this->payPlat->group_id;
    }

    /**
     * 获取订单号
     * @param int $type 1充值订单 2会员订单 3游戏充值订单
     * @return string
     */
    protected function getOrderNo($uid, $type = 1)
    {
        return date('YmdHis').$uid.$type.mt_rand(100, 999);
    }

    public static function getPayPageCacheKey($orderId, $orderType)
    {
        return "pay.pay_page.{$orderType}.{$orderId}";
    }

    /**
     * 发请求
     * @param $url
     * @param array $param
     * @param int $timeOut
     * @return mixed
     */
    protected function request($url, $param = [], $timeOut = 10)
    {
        $param = array_merge($param, [
            'gameid'=>$this->gameId,
            'groupid'=>$this->groupId,
            'clientip'=>get_client_ip(),
            'notify'=>getCurDomain().self::NOTIFY
        ]);
        $sign = md5($this->ivVersion.$this->iuserId.$this->iType.$this->appKey);
        $curlData = ['iversrion'=>$this->ivVersion,'iuserid'=>$this->iuserId,'itype'=>$this->iType,'idata'=>json_encode($param),'isign'=>$sign];
        $httph = curl_init();
        curl_setopt($httph, CURLOPT_HTTPHEADER, ['Content-type: application/json']);
        curl_setopt($httph, CURLOPT_URL, self::API_URL.$url);
        curl_setopt($httph, CURLOPT_POST, 1); //设置为POST方式
        //curl_setopt($httph, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
        curl_setopt($httph, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($httph, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($httph, CURLOPT_POSTFIELDS, json_encode($curlData));
        curl_setopt($httph, CURLOPT_AUTOREFERER, 1);
        curl_setopt($httph, CURLOPT_TIMEOUT, $timeOut);
        //curl_setopt($httph, CURLOPT_HEADER, 0);
        $rst = curl_exec($httph);
//		var_dump($rst);die;
        curl_close($httph);
        return json_decode($rst, true);
    }
}
